package org.bagou.xiangs.framework_mvvm.newer

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import org.bagou.xiangs.framework_mvvm.R

class MvvmAdapter : RecyclerView.Adapter<MvvmAdapter.MvvmViewHolder>() {

    private var dataList: ArrayList<WechatItem> = ArrayList()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MvvmViewHolder {
        val rootView =
            LayoutInflater.from(parent.context).inflate(R.layout.item_user_new_mvvm, parent, false)
        rootView.setBackgroundColor(Color.WHITE)
        return MvvmViewHolder(rootView)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: MvvmViewHolder, position: Int) {
        onViewLayoutSet(holder, position)
        holder.textView!!.text = dataList[position].itemName
        Glide.with(holder.imgView!!).load(dataList[position].itemIcon).into(holder.imgView!!)
    }


    fun addData(data: List<WechatItem>) {
        dataList.addAll(data)
        notifyDataSetChanged()
    }


    inner class MvvmViewHolder(rootView: View) : RecyclerView.ViewHolder(rootView) {
        var rootView: View? = null
        var textView: TextView? = null
        var imgView: ImageView? = null

        init {
            this.rootView = rootView
            this.textView = rootView.findViewById(R.id.item_text)
            this.imgView = rootView.findViewById(R.id.item_icon)

        }
    }

    // 设置控件样式
    private fun onViewLayoutSet(holder: MvvmViewHolder, position: Int) {
        val layoutParams: ViewGroup.LayoutParams = holder.rootView!!.layoutParams
        when (position) {
            0 -> {
                (layoutParams as? ViewGroup.MarginLayoutParams)?.topMargin = 30
                (layoutParams as? ViewGroup.MarginLayoutParams)?.bottomMargin = 30
            }
            itemCount - 1 -> (layoutParams as? ViewGroup.MarginLayoutParams)?.topMargin = 30
            else -> (layoutParams as? ViewGroup.MarginLayoutParams)?.topMargin = 1
        }
        holder.rootView!!.layoutParams = layoutParams
    }

}


